import Vue from "vue";
const req = (data, template_id, openid) => {
  return new Promise(async (resolve, reject) => {
    let uniLogin = await Vue.prototype.$getLoginInfo(); //登录uniapp

    const pushmsg = {
      touser: openid, //要发送人的 optionId
      // VtKvZQf9OYESSBB_vE3Jpom0_eShd1tiN5X1uFsrR9M
      template_id: template_id, //消息通知模板
      data: data,
      miniprogram_state: "trial",
      lang: "zh_CN",
    };

    uni.request({
      url: `https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=${uniLogin.access_token}`,
      data: JSON.stringify(pushmsg),
      method: "POST",
      success: (res) => {
        if (res.data.errcode == "43101") {
          reject({
            code: "43101",
            message: "未授权",
          });
        } else {
          resolve({
            code: "200",
            message: "发送成功",
            data: res.data,
          });
        }
      },
      fail(err) {
        reject({
          code: "500",
          message: err,
        });
      },
    });
  });
};

export default req;
